const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database()
  
  console.log('checkAdmin 被调用，openid:', wxContext.OPENID)
  
  try {
    const result = await db.collection('admins')
      .where({
        openid: wxContext.OPENID
      })
      .get()
    
    console.log('查询结果:', result)
    
    return {
      isAdmin: result.data.length > 0,
      openid: wxContext.OPENID,
      count: result.data.length
    }
  } catch (err) {
    console.error('查询管理员失败:', err)
    return {
      isAdmin: false,
      error: err.message,
      openid: wxContext.OPENID
    }
  }
}
